class Employee::CertificatesController < InheritedResources::Base
  # 员工证书分配Controller
  respond_to :json
  load_resource

  def index
    employee_certificates = Employee::Certificate.paginate(:page => params[:page])
    respond_with EmployeeCertificateJsonSerializer.index_json employee_certificates
  end

  def create
    employee_certificate = ::AddEmployeeCertificate.run permitted_params
    render CommandJsonSerializer.as_json employee_certificate
  end

  def show
    respond_with EmployeeCertificateJsonSerializer.as_json @certificate
  end

  def list_valid_certificates
    respond_with EmployeeCertificateJsonSerializer.index_json EmployeeCertificate.find_valid_certificates
  end

  def list_will_valid_certificates
    respond_with EmployeeCertificateJsonSerializer.index_json EmployeeCertificate.find_will_valid_certificates
  end

  def will_valid_certificates_count
    respond_with EmployeeCertificateJsonSerializer.count_json EmployeeCertificate.find_will_valid_certificates_by(params[:days])
  end

  def query_will_certificates
    respond_with EmployeeCertificateJsonSerializer.index_json EmployeeCertificate.query_will_valid_certificates(params[:begin_time], params[:end_time])
  end

  def update
    @certificate.update permitted_params
    respond_with @certificate
  end

  private
  def permitted_params
    params.permit(:certificate_id, :employee_id, :valid_from, :id, :number)
  end
end
